package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.UserDao;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.CorrectRespEvent;
import com.hangzhou.xyj.manager.ScreenManager;
import com.hangzhou.xyj.model.CityModel;
import com.hangzhou.xyj.model.type.ECorrectType;
import com.hangzhou.xyj.net.handler.UpdateZoneHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.UpdateZoneReq;
import com.hangzhou.xyj.ui.adapter.base.AbsBaseAdapter;
import com.hangzhou.xyj.ui.listener.CheckLoginListener;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.ToastUtil;

import java.util.List;


/**
 * Created by yaohuix on 2016/3/26  10:39
 * <p/>
 * adapter
 */
public class CityListAdapter extends AbsBaseAdapter<CityModel> {

    public CityListAdapter(Context context, List<CityModel> dataList) {
        super(context, dataList);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ((CityViewHolder) viewHolder).setData(mDataList.get(position));
        viewHolder.itemView.setOnClickListener(new CheckLoginListener() {
            @Override
            public void onClickView(View v) {
                final String zone = mDataList.get(position).getName();
                UpdateZoneReq updateZoneReq = new UpdateZoneReq(Integer.valueOf(CommonPreferences.getUserId(mContext)), zone);
                HttpLoader.getInstance().sendRequest(new UpdateZoneHandler(updateZoneReq, new HttpResponseListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(Object model) {
                        if ((Boolean) model) {
                            //更新应用中的信息
                            XYJApplication.getInstance().getUserInfo().setZone(zone);
                            //更新本地数据库中的数据
                            UserDao dao = new UserDao(mContext);
                            dao.addUser(XYJApplication.getInstance().getUserInfo());
                            //发送事件
                            CorrectRespEvent correctRespEvent = new CorrectRespEvent();
                            correctRespEvent.setValue(zone);
                            correctRespEvent.setTag(ECorrectType.ZONE.getValue());
                            EventBusUtil.postEvent(correctRespEvent);
                            ToastUtil.show("保存成功！");
                            ScreenManager.getInstance().currentActivity().finish();
                        }
                    }

                    @Override
                    public void onFail(String msg) {

                    }

                    @Override
                    public void onDone(String msg) {

                    }

                    @Override
                    public void onError() {

                    }
                }, 0));

            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new CityViewHolder(mLayoutInflater.inflate(R.layout.layout_city_item, viewGroup, false));
    }

    class CityViewHolder extends RecyclerView.ViewHolder {
        MyTextView mTvCity;//
        MyTextView mTvTop;//
        View mVUnderline;//

        public CityViewHolder(View itemView) {
            super(itemView);
            mTvCity = (MyTextView) itemView.findViewById(R.id.mtv_content);
            mTvTop = (MyTextView) itemView.findViewById(R.id.mtv_top);
            mVUnderline = itemView.findViewById(R.id.v_underline);
        }

        public void setData(CityModel data) {
            if (data.getIsAreaFirst()) {
                mTvTop.setText(data.getSortLetters());
                mTvTop.setVisibility(View.VISIBLE);
            } else {
                mTvTop.setVisibility(View.GONE);
            }
            mTvCity.setText(data.getName());
        }
    }
}
